package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/27 17:50
 */
public class C38 {


    public static String countAndSay(int n) {
        if (n == 1) return "1";

        String pre = countAndSay(n - 1);
        char[] str = pre.toCharArray();
        StringBuilder cur = new StringBuilder();

        int N = str.length;
        int cnt = 1;
        char last = str[0];
        for (int i = 1; i < N; i++) {
            if (str[i] != last) {
                cur.append(cnt).append(last);
                cnt = 0;
            }
            last = str[i];
            cnt++;
        }
        cur.append(cnt).append(last);
        return cur.toString();
    }


    // for test
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(i + " : " + countAndSay(i));
        }
    }

}
